package com.ml.admin.controller;

import com.ml.admin.pojo.vo.UserVO;
import com.ml.admin.service.UserService;
import com.ml.admin.utils.JwtUtil;
import com.ml.admin.utils.ResponseEnum;
import com.ml.admin.utils.ReturnResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

/**
 * @author ml
 * @date 2023/3/2 17:09
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 编辑用户信息
     * @param vo vo
     * @return success or failure
     */
    @PostMapping("/edituserinfo")
    public ReturnResponse editUserInfo(@RequestBody UserVO vo, HttpServletRequest request){
        try{
            String token = request.getHeader("token");
            if (StringUtils.isBlank(token)){
                return ReturnResponse.failure(ResponseEnum.VALID_ERROR);
            }
            String phone = JwtUtil.getUserInfo(token);
            if (StringUtils.isBlank(phone)){
                return ReturnResponse.failure(ResponseEnum.PARAM_NOT_COMPLETE);
            }
            vo.setPhone(phone);
            return userService.editUserInfo(vo);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }
    }
    /**
     * 查询用户信息
     * @param request token
     * @return 用户信息
     */
    @GetMapping("/queryuserinfo")
    public ReturnResponse queryUserInfo(HttpServletRequest request){
        try{
            String token = request.getHeader("token");
            if (StringUtils.isBlank(token)){
                return ReturnResponse.failure(ResponseEnum.PARAM_NOT_COMPLETE);
            }
            String phone = JwtUtil.getUserInfo(token);
            if (StringUtils.isBlank(phone)){
                return ReturnResponse.failure(ResponseEnum.PARAM_NOT_COMPLETE);
            }
            return userService.queryUserInfo(phone);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }
    }

    /**
     * 修改密码(设置密码)
     * @param vo 新密码
     * @return 成功 or 失败
     */
    @PostMapping("/updatepwd")
    public ReturnResponse updatePwd(@RequestBody UserVO vo,HttpServletRequest request) {
        try{
            String token = request.getHeader("token");
            if (StringUtils.isBlank(token)){
                return ReturnResponse.failure(ResponseEnum.PARAM_NOT_COMPLETE);
            }
            String phone = JwtUtil.getUserInfo(token);
            if (StringUtils.isBlank(phone)){
                return ReturnResponse.failure(ResponseEnum.PARAM_NOT_COMPLETE);
            }
            vo.setPhone(phone);
            return userService.updatePwd(vo);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }
    }

}
